package com.dib.neo.bkfs.fs;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

public class LimitQueue<E> implements Collection<E> {

  private int limit;

  private LinkedList<E> queue = new LinkedList<E>();

  public LimitQueue(int limit) {
    this.limit = limit;
  }


  public void offer(E e) {
    synchronized (queue) {
      if (queue.size() >= limit) {
        queue.poll();
      }
      queue.offer(e);
    }
  }

  public E get(int position) {
    return queue.get(position);
  }

  public E getLast() {
    return queue.getLast();
  }

  public E getFirst() {
    return queue.getFirst();
  }

  public int getLimit() {
    return limit;
  }

  public int size() {
    return queue.size();
  }

  @Override
  public boolean isEmpty() {
    return false;
  }

  @Override
  public boolean contains(Object o) {
    return false;
  }

  @Override
  public Iterator<E> iterator() {
    return queue.iterator();
  }

  @Override
  public Object[] toArray() {
    return queue.toArray();
  }

  @Override
  public <T> T[] toArray(T[] a) {
    return queue.toArray(a);
  }

  @Override
  public boolean add(E e) {
    offer(e);
    return true;
  }

  @Override
  public boolean remove(Object o) {
    return queue.remove(o);
  }

  @Override
  public boolean containsAll(Collection<?> c) {
    return queue.containsAll(c);
  }

  @Override
  public boolean addAll(Collection<? extends E> c) {
    return false;
  }

  @Override
  public boolean removeAll(Collection<?> c) {
    return queue.removeAll(c);
  }

  @Override
  public boolean retainAll(Collection<?> c) {
    return queue.retainAll(c);
  }

  @Override
  public void clear() {
    queue.clear();
  }
}
